$(document).ready(function(){  

var error_text = ('<span class="error"> *</span>');

$('#form_step_1').submit( function() {
		var flag_submit = true;
		$("span.error").remove();

		if ($.trim($("#albumname").val()) == "") {
		  $("#albumname").after(error_text);
		  flag_submit = false;
		}
	if(flag_submit){
		var content = $('#form_step_1').serializeArray();
		$.ajax({
			type: 'GET',
			url: './module/photo/function.php',
			data: {act:'new', val:content},
			dataType: 'json',
			success: function(data) {
				if(data.sta=='Error_0' || data.sta=='Error_1' || data.sta=='Error_2' || data.sta=='Error_3'){
					alert (data.text);
				} else if(data.sta=='Success_step1') {
					window.location.href = data.text;
				}
			},
			error: function (){ alert ('Cannot get function file'); }
		});
		return true;
	}
	return false;
});

$('#form_step_2').submit( function() {
	var content = $('#form_step_2').serializeArray();
	$.ajax({
		type: 'GET',
		url: './module/photo/function.php',
		data: {act:'new', val:content},
		dataType: 'json',
		success: function(data) {
			if(data.sta=='Error_0' || data.sta=='Error_1'){
				alert (data.text);
			} else if(data.sta=='Success_step2') {
				window.location.href = data.text;
			}
		},
		error: function (){ alert ('Cannot get function file'); }
	});
	return false;
});

$('#form_step_3').submit( function() {
	var content = $('#form_step_3').serializeArray();
	$.ajax({
		type: 'GET',
		url: './module/photo/function.php',
		data: {act:'new', val:content},
		dataType: 'json',
		success: function(data) {
			if(data.sta=='Error_0' || data.sta=='Error_1'){
				alert (data.text);
			} else if(data.sta=='Success_step3') {
				alert (data.text);
				//window.location.href = data.text;
			}
		},
		error: function (){ alert ('Cannot get function file'); }
	});
	return false;
});

});

function closethis(){
	$('.smak_onpage').slideUp('normal');
}
function togglethis(){
	$('#loadimage').slideToggle('normal');
}
function choosethis(linkthis,namethis){
	$('#icon_set').html('<img src="'+linkthis+'" />');
	$('#iconalbum').val(namethis);
	closethis();
}
function select_display(){
	if($('#display').val()=='0'){
		$('#listdisplayalbum').val('0');
		$('#list_display_full').slideUp();
	}
	else if($('#display').val()=='1'){
		$('#listdisplayalbum').val('1');
		$('#list_display_full').slideUp();
	}
	else if($('#display').val()=='2'){
		$('#listdisplayalbum').val('2');
		$('#list_display_full').slideUp();
	}
	else if($('#display').val()=='custom'){
		$('#list_display_full').slideDown();
	}
}

function open_friend_list(){
	closethis();
	$('#loadlistfriend').slideDown();
}

function open_group_list(){
	closethis();
	$('#loadlistgroup').slideDown();
}

function friend_to_album(){
	var current_list = $('#listdisplayalbum').val();
	
	if (current_list.indexOf(",")!= -1){
		var merg = current_list.substr(current_list.indexOf(","));
	} else {
		merg = ',';
	}
	var values_id = new Array();
	var values_name = new Array();
	$.each($("#loadlistfriend").find("input[name='select_friend']:checkbox:checked"), function() {
 		 values_id.push($(this).val());
		 values_name.push('<span class="span_name">'+$(this).attr('alt')+'</span>');
	});
	values_id = values_id.reverse().toString().replace(/,/g, ":");
	values_name = values_name.reverse().toString();
	closethis();
	if(values_id!=''){
		$('#friend_return').html(values_name);
		$('#listdisplayalbum').val(values_id+''+merg);
	} else {
		$('#friend_return').html('&nbsp;');
	}
}

function group_to_album(){
	var current_list = $('#listdisplayalbum').val();
	var num = '';
	if (current_list.indexOf(",") != -1){
		 num = current_list.indexOf(",");
	}
	var merg = current_list.substr(0,num);
	var values_id = new Array();
	var values_name = new Array();
	$.each($("#loadlistgroup").find("input[name='select_group']:checkbox:checked"), function() {
 		 values_id.push($(this).val());
		 values_name.push('<span class="span_name">'+$(this).attr('alt')+'</span>');
	});
	values_id = values_id.toString().replace(/,/g, ":");
	values_name = values_name.toString();
	closethis();
	if(values_id!=''){
		$('#group_return').html(values_name);
		$('#listdisplayalbum').val(merg+','+values_id);
	} else {
		$('#friend_return').html('&nbsp;');
	}
}
